<?php $this->load->view(TEMPLATE_NAME.'/front/header');?>
<?php $this->load->view(TEMPLATE_NAME.'/admin/side_left');?>
<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAPgPkVwQ5FlQH5LJuRF6rIRT2yXp_ZAY8_ufC3CFXhHIE1NvwkxRGSazNmUzBGkTALJNUad4UVGQOsQ"></script>
<script type="text/javascript">    
    google.load("maps", "2.x");
    google.load("search", "1");
</script>
<script type="text/javascript" src="<?php echo base_url()?>js/jquery-1.4.2.js"></script>

<style type="text/css">
.styletext{
    text-shadow: 0 0 0.2em #F87, 0 0 0.2em #F87;
    font-weight:bolder;
}
</style>
<script type="text/javascript" src="<?php echo $this->config->item('base_url')?>js/elabel.js"></script>
<script type="text/javascript">
    var map = null;
    var theListarea = '';
    var options = {};
    var lineCounter_ = 0;
    var colorIndex_ = 0;        
    var COLORS = [["orange", "#ff8800"]];
    var geocoder = null;
    
    // ====== Create a Client Geocoder ======
      var geo = new GClientGeocoder(); 

      // ====== Array for decoding the failure codes ======
      var reasons=[];
      reasons[G_GEO_SUCCESS]            = "Success";
      reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
      reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
      reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
      reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
      reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
      reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
      
      // ====== Geocoding ======
      function showAddress() {
        var search = document.getElementById("street_address").value;
        // ====== Perform the Geocoding ======        
        geo.getLocations(search, function (result)
          { 
            // If that was successful
            if (result.Status.code == G_GEO_SUCCESS) {
              // How many resuts were found
              document.getElementById("message").innerHTML = "Found " +result.Placemark.length +" results";
              // Loop through the results, placing markers
              for (var i=0; i<result.Placemark.length; i++) {
                var p = result.Placemark[i].Point.coordinates;
                var marker = new GMarker(new GLatLng(p[1],p[0]));
                document.getElementById("message").innerHTML += "<br>"+(i+1)+": "+ result.Placemark[i].address + marker.getPoint();
                map.addOverlay(marker);
              }
              // centre the map on the first result
              var p = result.Placemark[0].Point.coordinates;
              map.setCenter(new GLatLng(p[1],p[0]),14);
            }
            // ====== Decode the error status ======
            else {
              var reason="Code "+result.Status.code;
              if (reasons[result.Status.code]) {
                reason = reasons[result.Status.code]
              } 
              alert('Could not find "'+search+ '" ' + reason);
            }
          }
        );
      }
        
    
    $(document).ready(function(){
        map = new GMap2($("#map").get(0));
        var startMaps = new GLatLng(44.81874204150367,-93.32954406738281);
        map.setCenter(startMaps, 13);
        map.setZoom(15);
        map.disableDoubleClickZoom();
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        geocoder = new GClientGeocoder(); 
        <?php 
            $i = 0;
            foreach($mapping as $mappingData){
                $path = '[new GLatLng'.str_replace('#',', new GLatLng',$mappingData['path']).']';
                $latlon = explode('#',$mappingData['path']);
                //echo $latlon[0];
                ?>
                $('#network<?=$i?>').click(function(){
                    /*map.panTo(<?//=$latlon[0]?>);*/
                    map.setCenter(new GLatLng<?=$latlon[0]?>, 15);
                    return false;                    
                });
                 
                var polyData = <?php echo $path;?>; 
                var polygon_<?php echo $i?> = new GPolygon(polyData, '#ff8800', 2, 0.4, "#00000",0.1     );
                  map.addOverlay(polygon_<?php echo $i?>);
                GEvent.addListener(polygon_<?php echo $i?>, "mouseover", function(latlng, index) {
                    polygon_<?php echo $i?>.setFillStyle({color: '#008000', weight: 2,opacity:0.5});
                });
                GEvent.addListener(polygon_<?php echo $i?>, "mouseout", function(latlng, index) {
                    polygon_<?php echo $i?>.setFillStyle({color: '#00000', weight: 0.7,opacity:0.1});
                });
                //alert(polygon.getBounds().getCenter());
                var label = new ELabel(polygon_<?php echo $i?>.getBounds().getCenter(), '<?php echo $mappingData['name']?>', "styletext");
                  map.addOverlay(label)
            <?php
                $i++;
            }            
        ?>
            $("#find_by_name").submit(function(){
                find_network();
                return false;        
            });        
        });
        
        function find_network(){
            $.post("<?echo site_url('findnetwork/find_network')?>",{
                 network_name: $('#network_name').val(),                 
             }, function(xml) {                    
                    status = $("status",xml).text();
                    content = $("content",xml).text();
                    id = $("id",xml).text();
                    name = $("name",xml).text();
                    path = $("path",xml).text();
                    var i=1;
                    if (status == "ok"){
                        $("content",xml).find("network").each(function(i) {
                        /* add to tables */
                        id = $(this).find('id',xml).text();                       
                        name = $(this).find('name').text();
                        path = $(this).find('path').text();
                        /*
                        $("#message").append( i + 1 +". <a href='#' onclick='map.setCenter(new GLatLng"+path+", 15);return false;'>"+name+"</a><br />");
                        */
                        $("#message").append( i + 1 +". <a href='<?php echo site_url(profile/network_detail/"+id+")?>'>"+name+"</a><br />");
                        
                        });
                    }                    
            });
        } 
</script>
<div class="page noright adminpage adminuser">
    <h2>Find a Network</h2>
    <table>
    <tr>
        <td width="50%">        
        <form id="find_by_name">        
        Find by Network Name <a href="#">See all</a> <br />
        Type in Network Name<br />
        <input type="text" id="network_name" name="network_name" />
        <button>GO</button>
        </form>
        
        </td><td>
        
        <form onsubmit="showAddress(); return false;" action="#">
        Find by Address <a href="#">See all</a> <br />
        Input address to find on map below<br />
        <input id="street_address" type="text" name="street_address" />
        <button>GO</button>
        </form>
        </td>
    </tr><tr>
    <td colspan="2"><div id="message"></div></td>
    </tr><tr>
    <td colspan="2"><div id="map" style="width:400px;height:400px"></div></td>
    </tr><tr>
        <td>        
        Newest Networks <a href="#">See all</a> <br />
        <strong>10 of the newest networks created</strong> <br />
        <? $j = 0?>        
        <?foreach($mapping as $row){?>
        <!--<a id="network<?//=$j?>" href=""><?//=$row["name"]?></a><br />-->
        <a href="<?php echo base_url()?>index.php/profile/network_detail/<?=$row["id"]?>"><?=$row["name"]?></a><br />
        <?$j++; }?>
        </td><td>       
        No Network? Create it. <a href="#">See all</a> <br />
        Can't find the a Network to join?<br /> 
        Maybe the Network your are looking for has not been created yet - why not create it?<br />

        You can create a Network for a community, street, suburb, business park - basically anything that can be marked out on a map. <br />

        It's easy to do and only takes a moment.<br />

        <a href="<?php echo site_url('createnetwork')?>">Click here to create a Network</a>
        </td>
    </tr>
    
  
    
    </table>
<div class="clear"></div>
    <?php $this->load->view(TEMPLATE_NAME.'/admin/profile/footer');?>
</div><!-- page -->

<?php $this->load->view(TEMPLATE_NAME.'/admin/profile/sideright');?> 
